package com.dianping.cat.home.router.transform;

import com.dianping.cat.home.router.entity.DefaultServer;
import com.dianping.cat.home.router.entity.Domain;
import com.dianping.cat.home.router.entity.Group;
import com.dianping.cat.home.router.entity.GroupServer;
import com.dianping.cat.home.router.entity.Network;
import com.dianping.cat.home.router.entity.NetworkPolicy;
import com.dianping.cat.home.router.entity.RouterConfig;
import com.dianping.cat.home.router.entity.Server;
import com.dianping.cat.home.router.entity.ServerGroup;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.xml.sax.Attributes;











public class DefaultSaxMaker
  extends Object
  implements IMaker<Attributes>
{
  public DefaultServer buildDefaultServer(Attributes attributes) {
    String id = attributes.getValue("id");
/*  33 */     String weight = attributes.getValue("weight");
/*  34 */     String port = attributes.getValue("port");
/*  35 */     String enable = attributes.getValue("enable");
    DefaultServer defaultServer = new DefaultServer(id);

/*  38 */     if (weight != null) {
/*  39 */       defaultServer.setWeight(((Double)convert(Double.class, weight, Double.valueOf(0.0D))).doubleValue());
    }
    
/*  42 */     if (port != null) {
/*  43 */       defaultServer.setPort(((Integer)convert(Integer.class, port, Integer.valueOf(0))).intValue());
    }
    
/*  46 */     if (enable != null) {
/*  47 */       defaultServer.setEnable(((Boolean)convert(Boolean.class, enable, Boolean.valueOf(false))).booleanValue());
    }
    
/*  50 */     return defaultServer;
  }

  
  public Domain buildDomain(Attributes attributes) {
/*  55 */     String id = attributes.getValue("id");
/*  56 */     return new Domain(id);
  }



  
  public Group buildGroup(Attributes attributes) {
/*  63 */     String id = attributes.getValue("id");
/*  64 */     return new Group(id);
  }



  
  public GroupServer buildGroupServer(Attributes attributes) {
/*  71 */     String id = attributes.getValue("id");
/*  72 */     return new GroupServer(id);
  }



  
  public Network buildNetwork(Attributes attributes) {
/*  79 */     String id = attributes.getValue("id");
/*  80 */     return new Network(id);
  }



  
  public NetworkPolicy buildNetworkPolicy(Attributes attributes) {
/*  87 */     String id = attributes.getValue("id");
/*  88 */     String title = attributes.getValue("title");
/*  89 */     String block = attributes.getValue("block");
/*  90 */     String serverGroup = attributes.getValue("server-group");
/*  91 */     NetworkPolicy networkPolicy = new NetworkPolicy(id);

/*  93 */     if (title != null) {
/*  94 */       networkPolicy.setTitle(title);
    }
    
/*  97 */     if (block != null) {
/*  98 */       networkPolicy.setBlock(((Boolean)convert(Boolean.class, block, Boolean.valueOf(false))).booleanValue());
    }
    
/* 101 */     if (serverGroup != null) {
/* 102 */       networkPolicy.setServerGroup(serverGroup);
    }
    
/* 105 */     return networkPolicy;
  }

  
  public RouterConfig buildRouterConfig(Attributes attributes) {
/* 110 */     String backupServer = attributes.getValue("backup-server");
/* 111 */     String backupServerPort = attributes.getValue("backup-server-port");
/* 112 */     String startTime = attributes.getValue("startTime");
/* 113 */     String domain = attributes.getValue("domain");
/* 114 */     String endTime = attributes.getValue("endTime");
/* 115 */     RouterConfig routerConfig = new RouterConfig(domain);

/* 117 */     if (backupServer != null) {
/* 118 */       routerConfig.setBackupServer(backupServer);
    }
    
/* 121 */     if (backupServerPort != null) {
/* 122 */       routerConfig.setBackupServerPort(((Integer)convert(Integer.class, backupServerPort, Integer.valueOf(0))).intValue());
    }
    
/* 125 */     if (startTime != null) {
/* 126 */       routerConfig.setStartTime(toDate(startTime, "yyyy-MM-dd HH:mm:ss", null));
    }
    
/* 129 */     if (endTime != null) {
/* 130 */       routerConfig.setEndTime(toDate(endTime, "yyyy-MM-dd HH:mm:ss", null));
    }
    
/* 133 */     return routerConfig;
  }

  
  public Server buildServer(Attributes attributes) {
/* 138 */     String id = attributes.getValue("id");
/* 139 */     String port = attributes.getValue("port");
/* 140 */     String weight = attributes.getValue("weight");
/* 141 */     Server server = new Server();

/* 143 */     if (id != null) {
/* 144 */       server.setId(id);
    }
    
/* 147 */     if (port != null) {
/* 148 */       server.setPort(((Integer)convert(Integer.class, port, Integer.valueOf(0))).intValue());
    }
    
/* 151 */     if (weight != null) {
/* 152 */       server.setWeight(((Double)convert(Double.class, weight, Double.valueOf(0.0D))).doubleValue());
    }
    
/* 155 */     return server;
  }

  
  public ServerGroup buildServerGroup(Attributes attributes) {
/* 160 */     String id = attributes.getValue("id");
/* 161 */     String title = attributes.getValue("title");
/* 162 */     ServerGroup serverGroup = new ServerGroup(id);

/* 164 */     if (title != null) {
/* 165 */       serverGroup.setTitle(title);
    }
    
/* 168 */     return serverGroup;
  }

  
  protected <T> T convert(Class<T> type, String value, T defaultValue) {
/* 173 */     if (value == null || value.length() == 0) {
/* 174 */       return defaultValue;
    }
    
/* 177 */     if (type == Boolean.class || type == boolean.class)
/* 178 */       return (T)Boolean.valueOf(value); 
/* 179 */     if (type == Integer.class || type == int.class)
/* 180 */       return (T)Integer.valueOf(value); 
/* 181 */     if (type == Long.class || type == long.class)
/* 182 */       return (T)Long.valueOf(value); 
/* 183 */     if (type == Short.class || type == short.class)
/* 184 */       return (T)Short.valueOf(value); 
/* 185 */     if (type == Float.class || type == float.class)
/* 186 */       return (T)Float.valueOf(value); 
/* 187 */     if (type == Double.class || type == double.class)
/* 188 */       return (T)Double.valueOf(value); 
/* 189 */     if (type == Byte.class || type == byte.class)
/* 190 */       return (T)Byte.valueOf(value); 
/* 191 */     if (type == Character.class || type == char.class) {
/* 192 */       return (T)Character.valueOf(value.charAt(0));
    }
/* 194 */     return (T)value;
  }

  
  protected Date toDate(String str, String format, Date defaultValue) {
/* 199 */     if (str == null || str.length() == 0) {
/* 200 */       return defaultValue;
    }
    
    try {
/* 204 */       return (new SimpleDateFormat(format)).parse(str);
/* 205 */     } catch (ParseException e) {
/* 206 */       throw new RuntimeException(String.format("Unable to parse date(%s) in format(%s)!", new Object[] { str, format }), e);
    }
  }
}


/* Location:              /Users/hanfei/Documents/apache-tomcat-9.0.24/webapps/cat/WEB-INF/classes/!/com/dianping/cat/home/router/transform/DefaultSaxMaker.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       1.0.7
 */